<?php
	if (sizeof($_GET) == 1)
	{
		$code = $_GET['code'];

		$mysqli = new mysqli('localhost', 'root', 'project3', 'implauth');
		if ($mysqli->connect_errno)
		{
			echo "Database connection error!";
		}
		else
		{
			$res = $mysqli->query("select * from serverstba where code like '$code'");
			if ($res->num_rows == 1)
			{
				$ob = $res->fetch_object();
				$res->close();
				$id = sha1($ob->email);
				$secretkey = sha1(sha1($ob->email . time()));
				
				// mail them to the server
				
				require_once "Mail.php";

				$from = "Implicit%20Authentication%20Service";
				$to = "$ob->email";
				$subject = "Your Implicit Authentication credentials";
				$body = "Thank you for activating your account, your credentials are:\n\nid:\t$id\nkey:\t$secretkey\n\nPlease keep your id and key secret.";
				$host = "ssl://smtp.gmail.com";
				$port = "465";
				$username = "cs249project3"; 
				$password = "project3";

				$headers = array ('From' => $from,
				  'To' => $to,
				  'Subject' => $subject);
				$smtp = Mail::factory('smtp',
				  array ('host' => $host,
					'port' => $port,
					'auth' => true,
					'username' => $username,
					'password' => $password));

///////////////Uncomment the line below to actually send the email
				$mail = $smtp->send($to, $headers, $body);

				if (PEAR::isError($mail)) 
				{
				  echo("Error sending mail!<br /> " . $mail->getMessage());
				}
				else 
				{
					// insert them into servers and remove them from serverstba
					$email = $ob->email;
					$phone = $ob->phone;
					$sql = "insert into servers values ('$id', '$secretkey', '$email', " . ($phone == null ? "null)" : "'$phone')");
					$mysqli->query($sql);
					$mysqli->query("delete from serverstba where code like '$code'");
					$mysqli->close();

					// echo results to user
					echo "Thank you for activating your account.<br />An email with your id and key has been sent to you.";
				}




			}
			else
			{
				echo "Either your account has already been activated or you have not register!";
			}
		}
	}

?>





